﻿using System.Web.Mvc;
using VictorBlaga.DocumentManagement.Core.Services;
using VictorBlaga.DocumentManagement.Core.Models.Repositories;
using VictorBlaga.DocumentManagement.Core.Services.Impl;
using VictorBlaga.DocumentManagement.Helpers;
using VictorBlaga.DocumentManagement.Helpers.Mappers;
using VictorBlaga.DocumentManagement.Helpers.Mappers.Impl;
using VictorBlaga.DocumentManagement.Helpers.Validators;
using VictorBlaga.DocumentManagement.Helpers.Validators.Impl;
using VictorBlaga.DocumentManagement.Infrastructure.DataAccess;
using VictorBlaga.DocumentManagement.Core.Models;
using VictorBlaga.DocumentManagement.Models;

namespace VictorBlaga.DocumentManagement.Controllers
{
    public class AdministrationController : Controller
    {
        private readonly ICryptographerService _cryptoService;
        private readonly IUserRepository _userRepository;
        private readonly IWorkshopRepository _workshopRepository;
        private readonly IUserMapper _userMapper;
        private readonly IUserValidator _userValidator;
        private readonly ITeamRepository _teamRepository;
        private readonly ITeamMapper _teamMapper;
        private readonly ITeamValidator _teamValidator;
        private readonly IWorkshopMapper _workshopMapper;
        private IWorkshopValidator _workshopValidator;

        public AdministrationController(IUserRepository userRepository, IWorkshopRepository workshopRepository, ITeamRepository teamRepository, 
            IUserMapper userMapper, IWorkshopMapper workshopMapper, ITeamMapper teamMapper,
            IUserValidator userValidator, ICryptographerService cryptoService, ITeamValidator teamValidator, IWorkshopValidator workshopValidator)
        {
            _cryptoService = cryptoService;
            _workshopValidator = workshopValidator;
            _workshopMapper = workshopMapper;
            _teamValidator = teamValidator;
            _teamRepository = teamRepository;
            _userRepository = userRepository;
            _userValidator = userValidator;
            _workshopRepository = workshopRepository;
            _userMapper = userMapper;
            _teamMapper = teamMapper;
        }

        public AdministrationController()
        {
            _userRepository = new UserRepository();
            _cryptoService = new CryptographerService();
            _workshopRepository = new WorkshopRepository();
            _userMapper = new UserMapper(_workshopRepository, _cryptoService);
            _userValidator = new UserValidator(_userRepository);
            _teamRepository = new TeamRepository();
            _teamMapper = new TeamMapper(_userRepository);
            _teamValidator = new TeamValidator(_teamRepository);
            _workshopMapper = new WorkshopMapper(_userRepository, _teamRepository);
            _workshopValidator = new WorkshopValidator(_workshopRepository);
        }
        //
        // GET: /Administration/
        public ActionResult Index()
        {
            return View();
        }


        #region User Actions
        //
        // GET: /Administration/CreateUser
        public ActionResult CreateUser()
        {
            UserViewModel newUserViewModel = _userMapper.MapToViewModel(null);
            newUserViewModel.Workshops = _workshopRepository.GetAll();

            return View(newUserViewModel);
        }

        //
        // POST: /Administration/CreateUser
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateUser(UserViewModel userViewModel)
        {
            _userValidator.ViewModel = userViewModel;
            if(_userValidator.IsInsertValid())
            {
                var user = new User();
                _userMapper.MapToModel(userViewModel, user);
                _userRepository.Save(user);

                return RedirectToAction("Index");
            }

            ModelState.AddModelErrors(_userValidator.GetInsertValidationErrors());
            userViewModel.Workshops = _workshopRepository.GetAll();

            return View(userViewModel);
        }
        #endregion

        #region Workshop Actions
        public ActionResult CreateWorkshop()
        {
            WorkshopViewModel viewModel = _workshopMapper.MapToViewModel(null);
            viewModel.Managers = _userRepository.GetAvailableWorkshopManagers();
            viewModel.Teams = _teamRepository.GetAll();

            return View(viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateWorkshop(WorkshopViewModel viewModel)
        {
            _workshopValidator.ViewModel = viewModel;
            if(_workshopValidator.IsInsertValid())
            {
                Workshop workshop = new Workshop();
                _workshopMapper.MapToModel(viewModel, workshop);
                _workshopRepository.Save(workshop);

                return RedirectToAction("Index");
            }
            ModelState.AddModelErrors(_workshopValidator.GetInsertValidationErrors());
            viewModel.Managers = _userRepository.GetAvailableWorkshopManagers();
            viewModel.Teams = _teamRepository.GetAll();

            return View(viewModel);
        }
        #endregion

        #region Team Actions
        //
        // GET: /AdministrationCreateTeam
        public ActionResult CreateTeam()
        {
            TeamViewModel newTeamViewModel = _teamMapper.MapToViewModel(null);
            newTeamViewModel.Managers = _userRepository.GetAvailableTeamManagers();
            return View(newTeamViewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateTeam(TeamViewModel viewModel)
        {
            _teamValidator.ViewModel = viewModel;
            if(_teamValidator.IsInsertValid())
            {
                var team = new Team();
                _teamMapper.MapToModel(viewModel, team);
                _teamRepository.Save(team);

                return RedirectToAction("Index");
            }

            ModelState.AddModelErrors(_teamValidator.GetInsertValidationErrors());
            viewModel.Managers = _userRepository.GetAvailableTeamManagers();

            return View(viewModel);
        }
        #endregion
    }
}
